package com.bear.lock;

/** 计数器，多个线程处理一个共享变量
 * @author LiuShanshan
 * @version V1.0
 * @Description
 */
public class SynchronizedDemo1 {
    static  int counter = 0;
    static final Object object = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                synchronized (object){
                    counter++;
                }
            }
        }, "t1");
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                synchronized(object){
                    counter--;
                }
            }
        }, "t2");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(counter);

    }


}
